package controllers

import (
	"Dissertation/models"
	"sync"

	"github.com/gin-gonic/gin"
)

var wg sync.WaitGroup

// 测试
func Test(this *gin.Context) {
	this.String(200, "测试！")
}

type FindContext struct {
	Id   string `json:"id"`
	Data string `json:"data"`
}

// 查询
func Find(this *gin.Context) {
	data := FindContext{
		Id:   this.Query("id"),
		Data: this.Query("data"),
	}
	if data.Id != "" {
		if data.Data != "" {
			switch data.Id {
			case "学号":
				//学号查询
				err, user := models.FIndNumber(data.Data)
				models.SendMessage(err, this, user)
			case "姓名":
				//姓名查询
				err, user := models.FIndName(data.Data)
				models.SendMessage(err, this, user)

			case "性别":
				//性别查询
				err, user := models.FIndSex(data.Data)
				models.SendMessage(err, this, user)

			case "班级":
				//班级查询
				err, user := models.FIndClass(data.Data)
				models.SendMessage(err, this, user)
			default:
				this.String(500, "选择器错误，请稍后再试！")
			}

		} else if data.Id == "5" {
			//查询所有数据
			err, user := models.FindAll()
			models.SendMessage(err, this, user)
		}

	} else {
		this.String(500, "信息错误，请重新提交！")
	}

}

// 修改
func Revise(this *gin.Context) {
	number := this.Query("number")
	var user models.User
	err := this.ShouldBind(&user)
	if err != nil {
		this.String(500, err.Error())
	}

	err = models.Update(user, number)
	if err != nil {
		this.String(500, err.Error())
	} else {
		this.String(200, "修改成功！")
	}

}

// 删除
func Delete(this *gin.Context) {
	Type := this.Query("type")
	msg := this.Query("Data")
	err := models.Delete(Type, msg)
	if err != nil {
		this.String(500, err.Error())
	} else {
		this.String(200, "删除成功！")
	}
}

// 添加
func UserAdd(this *gin.Context) {
	var user models.User
	this.ShouldBind(&user)
	err := models.Add(user)
	if err != nil {
		this.String(500, err.Error())
	} else {
		this.String(200, "添加成功！")
	}
}
